var util=require('../../utils/util.js')
Page({
  data: {
    redpackets:[
      { id: 0, price1: 5, price2: 30, title: "生鲜果蔬红包", time: "2017/08/02", phone: "限生鲜果蔬频道使用。仅限18316588252用户使用。" },
      { id: 1, price1: 10, price2: 40, title: "生鲜果蔬红包", time: "2017/08/02", phone: "限生鲜果蔬频道使用。仅限18316588252用户使用。",  }
    ],
    packetstate: [{ name: "未使用", value: 0 }, { name: "已使用", value: 1 }, { name: "已过期", value: 2 }],
    redacts: [
      { id: 2, title: "全部商品9.5折", summary: "庆祝双十一，促销大打折", redtype: 0, startTime: "2019-11-10 00:00:00", endTime: "2019-11-12 00:00:00", discount: 95 },
      { id: 1, title: "全部商品满减", summary: "满30减5元，满60减15元", redtype: 1, startTime: "2019-11-10 00:00:00", endTime: "2019-11-12 00:00:00", full: '30/50/100', reduce: '3/6/12', 'getstate': 1},
    ],
    sendtypes: [{ name: '每人一次', value: '0', checked: 'true' },
    { name: '每人每天一次', value: '1' },
    { name: '不限次', value: '2' },],
    currentTab:0,

    amount:0,
    shopid:0
  },
  onLoad:function(options){
    var amount=options.amount;
    var shopid=options.shopid;
    if (shopid && shopid!=""  && shopid!=0){
      this.setData({shopid:shopid})
    }
    if (amount && amount != '' && amount!=0){
      this.setData({ amount: amount })
    }
    
    this.getonlineactbyshop();
    this.getmyredpacket();

    var currentTab = options.currentTab
    if (currentTab){
      this.setData({ currentTab: currentTab})
    }

  },
  swichNav: function (e) {
    var that = this;
    if (this.data.currentTab === e.target.dataset.current) {
      return false;
    } else {
      that.setData({
        currentTab: e.target.dataset.current,
      })
    }
  }, 
  getmyredpacket:function(){
    var url ="redpacket/list"
    var that = this;
    util.fetchGet(
      url,
      function(ret){
        var list=ret.data;
        that.setredpacket(list);
      }
    )
  },
  setredpacket:function(list){
    for (var i = 0; i < list.length; i++) {
      if (list[i].redtype == 1) {
        var reduction = list[i].reduction;
        var full = ""; var reduce = "";
        for (var j = 0; j < reduction.length; j++) {

          full += reduction[j].full
          reduce += reduction[j].reduce
          if (j != reduction.length - 1) {
            full += "/";
            reduce += "/";
          }
        }
        list[i].full = full;
        list[i].reduce = reduce;
      }
      this.setpacketstate(list[i]);
    }
    list.sort(function(a,b){
      return a.state-b.state;
    })
    this.setData({
      redpackets: list
    })
  },  
  setpacketstate:function(pac){
    var packetstate = this.data.packetstate;
    for (var i = 0; i < packetstate.length;i++){    
      if (pac.state == packetstate[i].value){
        pac.statename = packetstate[i].name;
      }
    }    
  },
  getonlineactbyshop:function(){
    var shopid=this.data.shopid;
    if (shopid==0){
      return;
    }
    var url ="redpacket/getonlineact?shop="+shopid;
    var that = this;
   // var url = "/red/listredact";
    util.fetchGet(
      url,
      function (ret) {
        var list = ret.data;
        that.setact(list);
      }
    )
  },
  setact:function(list){
    for (var i = 0; i < list.length; i++) {
      if (list[i].redtype == 1) {
        var reduction = list[i].reduction;
        var full = ""; var reduce = "";
        for (var j = 0; j < reduction.length; j++) {

          full += reduction[j].full
          reduce += reduction[j].reduce
          if (j != reduction.length - 1) {
            full += "/";
            reduce += "/";
          }
        }
        list[i].full = full;
        list[i].reduce = reduce;
      }
      list[i].sendtype = this.getsendtype(list[i].sendtype);
    }
    list.sort(function(a,b){
      return a.isTake-b.isTake;
    })
    this.setData({
      redacts: list
    })
  },
  getsendtype: function (sendtype) {
    var sendtypes = this.data.sendtypes;
    for (var i = 0; i < sendtypes.length; i++) {
      if (sendtype == sendtypes[i].value) {
        return sendtypes[i].name
      }
    }
  },
  getmypacket:function(){

  },
  takeredpacketformact:function(e){
    var that=this;
    var id=e.currentTarget.dataset.id;
    var url ="/redpacket/getpacketfromact?actid="+id;
    util.fetchGet(
      url,
      function(ret){
        var data=ret.data;
        var redpackets = that.data.redpackets;
        redpackets.push(data);
        that.setredpacket(redpackets);

        var redacts = that.data.redacts;
        var isswitch=true;
        for (var i=0;i<redacts.length;i++){
          if (redacts[i].id==id){
            redacts[i].isTake=1;
          }
          if (redacts[i].isTake==0){
            isswitch=false;
          }
        }
        that.setData({ 
          redacts: redacts,          
          })
        if (isswitch){//所有都领取口 切换tab
          that.setData({currentTab:0})
        }
        util.showinfo("卡券已领取");
      }
    )
  },
  useredpacket:function(e){
    var id = e.currentTarget.dataset.id;
    var redpackets = this.data.redpackets;
    var redpackage = null;
    for (var i = 0; i < redpackets.length; i++) {
      if (id == redpackets[i].id) {
        redpackage = redpackets[i];
      }
    }
    if (this.data.amount==0){
      wx.navigateTo({
        url: '../index/index?id=' + redpackage.shop,
      })
      return;
    }
    

    if (redpackage==null) return ;
    if (redpackage.redtype == 0) {

    } else if (redpackage.redtype == 1) {
      var canuse = false;
      for (var i = 0; i < redpackage.reduction.length; i++) {
        if (redpackage.reduction[i].full < this.data.amount / 100) {
          canuse = true;
        }
      }
      if (!canuse) {
        util.showinfo('你的订单金额不符合卡券使用要求');
        return;
      } 
    }
    var pages = getCurrentPages();//页面指针数组 
    var prepage = pages[pages.length - 2];//上一页面指针 
    if (prepage.route != 'pages/submitOrder/submitOrder'
      && prepage.route != 'pages/orderDetail/orderDetail') {
      return;
    }

    prepage.setData({
      redpackage: redpackage
    })
    wx.navigateBack();
  }
})